package com.baseinfo.action;

import com.framework.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 *  读取图片
 */
@Controller
@RequestMapping("/imageManage")
public class ImageController {
    Logger logger = Logger.getLogger(ImageController.class);

    @RequestMapping(value = "getImage", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public Object getImage (HttpServletRequest request, HttpServletResponse response, String rootFile, Long id, String fileName) throws IOException {
        if (StringUtil.isNotEmpty(fileName)) {
            try {
                String path = "F:\\cardolar\\picture" + File.separator + rootFile + File.separator + id+File.separator+fileName;
                //读取图片
                FileInputStream in=new FileInputStream(path);
                int i=in.available();
                byte[]data=new byte[i];
                in.read(data);
                in.close();
                //写图片
                OutputStream outputStream=new BufferedOutputStream(response.getOutputStream());
                outputStream.write(data);
                outputStream.flush();
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
